package com.csj.mvvm.ui.base

import android.app.Dialog
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentActivity
import com.csj.mvvm.R
import com.csj.mvvm.event.EventCode
import com.csj.mvvm.event.EventMessage
import com.csj.mvvm.utils.CustomDialog
import com.csj.mvvm.utils.LogUtil
import com.csj.mvvm.utils.NightModeUtil
import com.csj.mvvm.utils.StatusBarUtil2
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

/**
 * View不使用反射
 */
abstract class BaseBindActivity<VB : ViewDataBinding> : AppCompatActivity() {
    lateinit var mContext: FragmentActivity
    lateinit var v: VB

    private var loadingDialog: Dialog? = null
    private var builder: CustomDialog.Builder? = null
    var canCancel = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initResources()

        if (needAnim()) {
            overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left)
        }
        if (useStatusBar()) {
            // 设置为无标题栏
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
            // 沉浸状态栏
            //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
            StatusBarUtil2.setRootViewFitsSystemWindows(this, true)
            //设置状态栏透明
            StatusBarUtil2.setTranslucentStatus(this, !NightModeUtil.isNightMode(this))
        }

        v = DataBindingUtil.setContentView(this, getLayoutId())
        v.lifecycleOwner = this

        mContext = this
        init()
        initData()
        initClick()
        LogUtil.d(getClassName())
    }

    /**
     * 防止系统字体影响到app的字体
     *
     * @return
     */
    open fun initResources(): Resources? {
        val res: Resources = super.getResources()
        val config = Configuration()
        config.setToDefaults()
        res.updateConfiguration(config, res.displayMetrics)
        return res
    }

    override fun onDestroy() {
        super.onDestroy()
        if (needAnim()) {
            overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right)
        }
        EventBus.getDefault().unregister(this)
    }

    //事件传递
    @Subscribe
    fun onEventMainThread(msg: EventMessage) {
        handleEvent(msg)
    }

    open fun getClassName(): String? {
        val className = "BaseBindActivity"
        try {
            return javaClass.name
        } catch (e: Exception) {
        }
        return className
    }

    open fun needAnim(): Boolean {
        return true
    }

    open fun useStatusBar(): Boolean {
        return true
    }

    abstract fun getLayoutId(): Int

    abstract fun initClick()

    abstract fun initData()

    private fun init() {
        EventBus.getDefault().register(this)
    }

    fun showLoading(msg: String) {
        if (loadingDialog == null) {
            builder = CustomDialog.Builder(mContext).setCancelable(canCancel)
            loadingDialog = builder?.createLoading(msg)
        }
        builder?.startAnimation()
        loadingDialog?.show()
    }

    fun showLoading() {
        if (loadingDialog == null) {
            builder = CustomDialog.Builder(mContext).setCancelable(canCancel)
            loadingDialog = builder?.createLoading("")
        }
        builder?.startAnimation()
        loadingDialog?.show()
    }

    fun dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog?.dismiss()
        }
    }

    /**
     * 消息、事件接收回调
     */
    open fun handleEvent(msg: EventMessage) {
        if (msg.code == EventCode.LOGIN_OUT) {
            finish()
        }
    }

    /**
     * 控件自封装实现关闭页面的方法
     * 布局文件添加android:onclick="xxx"
     */
    open fun onClickFinish(v: View?) {
        finish()
    }

}